// 17.电话号码的字母组合
package LeetcodeHot100;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution017 {
    List<String> result = new ArrayList<>();

    public List<String> letterCombinations(String digits) {
        if (digits.length() == 0)
            return new ArrayList<>();

        Map<Integer, char[]> map = new HashMap<>();
        map.put(2, new char[] { 'a', 'b', 'c' });
        map.put(3, new char[] { 'd', 'e', 'f' });
        map.put(4, new char[] { 'g', 'h', 'i' });
        map.put(5, new char[] { 'j', 'k', 'l' });
        map.put(6, new char[] { 'm', 'n', 'o' });
        map.put(7, new char[] { 'p', 'q', 'r', 's' });
        map.put(8, new char[] { 't', 'u', 'v' });
        map.put(9, new char[] { 'w', 'x', 'y', 'z' });

        BackTrace(digits, map, new StringBuilder(), 0);

        return result;
    }

    public void BackTrace(String digits, Map<Integer, char[]> map, StringBuilder sb, int cur) {
        if (cur == digits.length()) {
            result.add(sb.toString());
            return;
        }
        for (char c : map.get(digits.charAt(cur) - '0')) {
            sb.append(c);
            BackTrace(digits, map, sb, cur + 1);
            sb.deleteCharAt(sb.length() - 1);
        }
    }
}
